import abc
import struct

import numpy


class math:

    def __init__(self):
        pass

    @staticmethod
    def float32_to_bits(val):
        # 将浮点数打包为4字节（32位）的二进制表示
        return struct.unpack('!I', struct.pack('!f', val))[0]

    @staticmethod
    def float64_to_bits(val):
        # 将浮点数打包为8字节（64位）的二进制表示
        return struct.unpack('!Q', struct.pack('!d', val))[0]

    @staticmethod
    def bit_to_float32(bits_32):
        return numpy.float32.frombits(bits_32)

    @staticmethod
    def bit_to_float64(bits_64):
        try:
            # 将整数转换为字节序列（大端序）
            bytes_representation = bits_64.to_bytes(8, byteorder='big', signed=True)

            # 使用NumPy的frombuffer方法将字节序列转换为float64数组
            # 注意：这里使用了reshape(1)来确保我们得到一个单一的浮点数而不是一个包含多个元素的数组
            float_value = numpy.frombuffer(bytes_representation, dtype=numpy.float64).reshape(1)[0]
            return float_value
        except Exception as e:
            print(e)

    @staticmethod
    def bigEndianUint16(bytes_read) -> numpy.uint16:
        return struct.unpack('!H', bytes_read)[0]

    @staticmethod
    def bigEndianUInt32(bytes_read) -> numpy.uint32:
        return int.from_bytes(bytes_read, byteorder='big', signed=True)

    @staticmethod
    def bigEndianUInt64(bytes_read) -> numpy.uint64:
        return int.from_bytes(bytes_read, byteorder='big', signed=True)

    @staticmethod
    def for_skip_range(rg,data, fn):
        skip_i = []
        for i in rg:
            if i in skip_i:
                continue
            if fn(i,data):
                skip_i.append(i + 1)

